package com.healthcare.client.controller;


import com.github.pagehelper.PageInfo;
import com.healthcare.client.form.ApplyReferralForm;
import com.healthcare.client.pojo.Patient;
import com.healthcare.client.pojo.Referral;
import com.healthcare.client.pojo.User;
import com.healthcare.client.service.ReferralService;
import com.healthcare.client.utils.QRCodeGenerator;
import com.healthcare.client.utils.SessionUtils;
import com.healthcare.client.utils.WordUtils;
import com.healthcare.client.vo.AdminConsultationlListVo;
import com.healthcare.client.vo.DoctorConsultationlListVo;
import com.healthcare.client.vo.DoctorMonadVo;
import com.healthcare.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 转诊申请 前端控制器
 * </p>
 *
 * @author 新华医院转诊联盟
 * @since 2020-04-13d
 */
@Api(tags = "前台转诊管理")
@RestController
@RequestMapping("/client/referral")
public class ReferralController {

    @Autowired
    private ReferralService referralService;

    @ApiOperation("获取转诊申请需要的所有信息")
    @GetMapping("applymessage")
    public Result getReferralApplyInfo(){
        Map<String,Object> map =  referralService.getReferralApplyInfo();
        return Result.ok().data("datas",map);
    }

    @ApiOperation("新增转诊信息--重新提交")
    @PostMapping("addReferralInfo")
    public Result addReferralInfo(
            @ApiParam(name = "ApplyReferralForm", value = "提交的转诊信息", required = true)
            @RequestBody ApplyReferralForm applyReferralForm){
        Map<String,Object> map = referralService.addReferralInfo(applyReferralForm);
        return Result.ok().data("datas",map);
    }

    @ApiOperation("查询转诊列表数据")
    @GetMapping("getReferralListVo/{page}/{size}/{identity}/{sortType}/{patientname}")
    public Result getReferralListVo(@PathVariable Integer page,@PathVariable Integer size,@PathVariable Integer identity,@PathVariable Integer sortType,@PathVariable String patientname){
       PageInfo<AdminConsultationlListVo> pageInfo = referralService.getReferralListVo(page,size,identity,sortType,patientname);
        return  Result.ok().data("datas",pageInfo);
    }

    @ApiOperation("根据转诊id查询转诊单数据")
    @PostMapping("getDoctorMonadVoList/{referralId}")
    public Result getDoctorMonadVoList(
            @ApiParam(name = "referralId", value = "转诊id", required = true)
            @PathVariable Integer referralId){
        List<DoctorMonadVo> list =  referralService.getDoctorMonadVoList(referralId);
        return Result.ok().data("datas",list);
    }

    @ApiOperation("取消转诊预约")
    @DeleteMapping("deleteReferral/{id}")
    public  Result deleteReferralInfo(
            @ApiParam(name = "id", value = "转诊id", required = true)
            @PathVariable  Integer id){
        referralService.deleteReferralInfo(id);
        return Result.ok();
    }


    @ApiOperation("获取小助手审核操作页面需要的信息")
    @GetMapping("getReferralAuditInformation/{id}")
    public Result getReferralAuditInformation(
            @ApiParam(name = "id", value = "转诊id", required = true)
            @PathVariable  Integer id){
        Map<String,Object> map =  referralService.getReferralAuditInformation(id);
         return Result.ok().data("datas",map);
    }

    @ApiOperation("审核操作")
    @PutMapping("referralAuditInformation/{type}")
    public  Result referralAuditInformation(
            @ApiParam(name = "referral", value = "转诊对象", required = true)
            @RequestBody  Referral referral,
            @ApiParam(name = "type", value = "0 - 1 用来区分是通过还是拒绝", required = true)
            @PathVariable Integer type){
        referralService.referralAuditInformation(referral,type);
        return  Result.ok();
    }

    @ApiOperation("获取重新提交转诊申请得数据")
    @PutMapping("retryInfo/{referralId}")
    public Result retryInfo(@PathVariable Integer referralId){
        Map<String,Object> map =   referralService.getReferralAuditInformationInfo(referralId);
        return Result.ok().data("datas",map);
    }

    @ApiOperation("完成转诊 -- 撤销")
    @PutMapping("accomplishReferral/{referralId}/{type}")
    public Result accomplishReferral(@PathVariable Integer referralId,@PathVariable Integer type){
        referralService.accomplishReferral(referralId,type);
        return Result.ok();
    }

    @ApiOperation("打印转诊单")
    @GetMapping("exportReferralWord/{referralId}")
    public void exportReferralWord(HttpServletRequest request, HttpServletResponse response,
                               @ApiParam(name = "referralId", value = "转诊id", required = true)
                               @PathVariable Integer referralId){
        List<DoctorMonadVo> list =  referralService.getDoctorMonadVoList(referralId);
        DoctorMonadVo doctorMonadVo =  list.get(0);
        System.out.println(doctorMonadVo);
        //二维码内的信息
        String info = "http://jihuiyi.cn/hvs/";
        byte[] qrcode = null;
        WordUtils wordUtil=new WordUtils();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("${age}", doctorMonadVo.getAge());
        params.put("${patientname}", doctorMonadVo.getPatientname());
        params.put("${mobile}", doctorMonadVo.getMobile());
        String sex = "男";
        if(doctorMonadVo.getSex() == 1){
             sex = "女";
        }
        params.put("${sex}", sex);
        params.put("${height}", doctorMonadVo.getHeight()+" cm");
        params.put("${weight}", doctorMonadVo.getWeight()+" KG");
        params.put("${hospitalnamecommon}", "上海新华医院");
        params.put("${shifttoadminoffice}", doctorMonadVo.getAdminofficename());
        params.put("${groupDoctor}", doctorMonadVo.getGroupDoctor());
        params.put("${outpatientservicename}", doctorMonadVo.getOutpatientservicename());
        params.put("${groupTime}", doctorMonadVo.getGroupTime());
        params.put("${hospitalname}", doctorMonadVo.getHospitaldictionaryname());
        params.put("${adminofficename}",doctorMonadVo.getAdminofficenameuser());
        params.put("${username}", doctorMonadVo.getUsername());
        params.put("${appointmentTime}", doctorMonadVo.getAppointmentTime());

        params.put("${chiefComplaint}", doctorMonadVo.getChiefComplaint());
        params.put("${physicalExamination}",doctorMonadVo.getPhysicalExamination());
        params.put("${problemToBeSolved}", doctorMonadVo.getProblemToBeSolved());
        params.put("${operator}", "联盟小助手");
        params.put("${groupRemark}", doctorMonadVo.getGroupRemark());
        try{
            qrcode = QRCodeGenerator.getQRCodeImage(info, 100, 85);
            Map<String,Object> header2 = new HashMap<String, Object>();
            header2.put("width", 100);
            header2.put("height", 85);
            header2.put("type", "jpg");
            header2.put("content", qrcode);
            params.put("${header}",header2);
            List<String[]> testList = new ArrayList<String[]>();
            String path="C:/Users/lsh/Desktop/paintmessage.docx";  //模板文件位置
            String fileName= new String("测试生成转诊单.docx".getBytes("UTF-8"),"iso-8859-1");    //生成word文件的文件名
            wordUtil.getWord(path,params,testList,fileName,response);

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

